package com.kuanter.adapter;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.kuanter.kuanterauto.MainActivity;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.AccountList;
import com.kuanter.kuanterauto.entity.CardInfo;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.ImageLoader;
import com.kuanter.kuanterauto.utils.ListViewUtil;
/**
 * 卡片信息适配器
 * @author soup
 *
 */
public class Lv_kpxxAdapter extends BaseAdapter {

	private List<CardInfo> carinfos = null;
	private Context context = null;
	private Lv_jffromAdapter fromAdapter = null;
	private boolean mBusy = false;
	private ImageLoader mImageLoader;
	private DataLoader dataLoader=null;
	private Handler handler=null;

	public void setFlagBusy(boolean busy) {
		this.mBusy = busy;
	}

	public Lv_kpxxAdapter(Context context, List<CardInfo> carinfos,Handler handler) {
		this.carinfos = carinfos;
		this.context = context;
		mImageLoader = new ImageLoader(context);
		dataLoader=new DataLoader(context);
		this.handler=handler;
	}

	public ImageLoader getImageLoader() {
		return mImageLoader;
	}

	@Override
	public int getCount() {
		return carinfos.size();
	}

	@Override
	public Object getItem(int position) {
		return carinfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	// 请求客户卡片数据 cardInfo/query_card
	// {"status":true,
	// "cardInfoList":[{
	// "accountList":[{"accountTypeName":"积分","balance":60,"customerGroupName":"人保郑州电子商务部","expireTime":"2013-10-12"}],
	// "activationTime":"2012-10-25 18:07:32","cardNumber":"11001261","cardPicPath":"\/resources\/cardTypePicture\/31859420121211094600.png","cardState":"正常"}]}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		
		if (convertView == null) {
			holder = new Holder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.lv_kpxx_item, null);
			holder.iv_kpxx_item = (ImageView) convertView
					.findViewById(R.id.iv_kpxx_item);
			holder.tv_item_cardnum = (TextView) convertView
					.findViewById(R.id.tv_item_cardnum);
			holder.tv_item_cardnum2 = (TextView) convertView
					.findViewById(R.id.tv_item_cardnum2);
			holder.lv_jffrom = (ListView) convertView
					.findViewById(R.id.lv_jffrom);
			holder.bt_item_gs = (Button) convertView
					.findViewById(R.id.bt_item_gs);
			
			
			

			convertView.setTag(holder);
		} 
		else {
			holder = (Holder) convertView.getTag();
		}
			
		final CardInfo cardinfo = carinfos.get(position);
		
		String num=cardinfo.getCardNumber();
		
		if(num!=null&&!num.equals("")){
		holder.tv_item_cardnum2.setText(num);
		}else{
			holder.tv_item_cardnum2.setText("");
		}
		
		if (!mBusy) {
			mImageLoader.DisplayImage(
					MainConstants.HOST_img + cardinfo.getCardPicPath(),
					holder.iv_kpxx_item, false);
		} else {
			mImageLoader.DisplayImage(
					MainConstants.HOST_img + cardinfo.getCardPicPath(),
					holder.iv_kpxx_item, false);
		}
		
		if (cardinfo.getAccoutlists() != null) {
			List<AccountList> accoutlists = cardinfo.getAccoutlists();
			if(accoutlists.size()>0){
			fromAdapter = new Lv_jffromAdapter(context, accoutlists);
			holder.lv_jffrom.setVisibility(View.VISIBLE);
			holder.lv_jffrom.setAdapter(fromAdapter);
			ListViewUtil.setListViewHeightBasedOnChildren(holder.lv_jffrom);
			}else{
				holder.lv_jffrom.setVisibility(View.GONE);
			}
		}else{
			holder.lv_jffrom.setVisibility(View.GONE);
		}
		
		
		holder.bt_item_gs.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle("是否挂失该卡?");
				builder.setPositiveButton("挂失",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0,
									int arg1) {
								List<NameValuePair> params = new ArrayList<NameValuePair>();
								params.add(new BasicNameValuePair("cardInfo.cardNumber", cardinfo.getCardNumber()));
								params.add(new BasicNameValuePair("cardInfo.userId", MainActivity.ID+""));
								dataLoader.DisplayData(MainConstants.HOST+"cardInfo/report_lost_card", params, handler, "",false);
							}
						});

				builder.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0,
									int arg1) {
							}
						});

				builder.create().show();
				
			}
		});

		

		return convertView;
	}

	static class Holder {
		ImageView iv_kpxx_item;
		TextView tv_item_cardnum;
		TextView tv_item_cardnum2;
		ListView lv_jffrom;
		Button bt_item_gs;
	}

}
